﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.TcCommands.CApAWCManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using ActiveWorkspaceHosting;
using COMMANDPARAMSLib;
using EngineeringInternalExtension;
using System;
using System.Windows;
using System.Windows.Input;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;
using UiInternalControls;


namespace DnProcessSimulateCommands.TcCommands;

internal class CApAWCManager : CApAWCHostSessionManager
{
  private CApAWCOpenStudyParameters m_openStudyParams;
  private bool m_loadStudyWasCanceled;
  private bool m_isDuringLoadStudy;
  private const string CLS_FUNCTION_NAME = "PSHostedOpenStudy";
  private const string REG_ENTRY_DEBUG_FULL_AWC_URL = "OpenStudyAddress Debug";

  public override string DebugEntry => "OpenStudyAddress Debug";

  public override string ComponentId => "PSHostedOpenStudy";

  public override string GetComponentUrlSuffix(TxAWSConnectionManagerEx connectionManager)
  {
    string componentUrlSuffix = (string) null;
    if (connectionManager.GetIsTcmaProvidedByAw())
      componentUrlSuffix = "?ah=true#/tcmaStudyManager?";
    return componentUrlSuffix;
  }

  public bool LoadStudyWasCanceled
  {
    get => this.m_loadStudyWasCanceled;
    set => this.m_loadStudyWasCanceled = value;
  }

  public bool IsDuringLoadStudy
  {
    get => this.m_isDuringLoadStudy;
    set => this.m_isDuringLoadStudy = value;
  }

  protected override void RegisterServicesInternal()
  {
    TxActiveWorkspaceHostManagerEx.Instance.RegisterUniqueService((TxBaseHostingService) new TxHostLoadStudySvc(this.GetLoadStudyHandler()));
  }

  protected override void UnregisterServicesInternal()
  {
    TxActiveWorkspaceHostManagerEx.Instance.UnregisterUniqueService((TxBaseHostingService) new TxHostLoadStudySvc(this.GetLoadStudyHandler()));
  }

  private Action<TxHostLoadStudyMsg> GetLoadStudyHandler()
  {
    return TxApplication.PlatformMode != 1 ? new Action<TxHostLoadStudyMsg>(this.LoadStudyInPS) : new Action<TxHostLoadStudyMsg>(this.LoadStudyInPSViewer);
  }

  internal override void OnClientCompleted()
  {
    if (this.LoadProcessToTune())
      return;
    int num = (int) MessageBox.Show(DnProcessSimulateCommands.TcCommands.TcNavigator.StringTable.AWC_OPEN_STUDY_ERROR, DnProcessSimulateCommands.TcCommands.TcNavigator.StringTable.AWC_NAME, MessageBoxButton.OK, MessageBoxImage.Hand);
  }

  private void LoadStudyInPS(TxHostLoadStudyMsg input)
  {
    this.ClearParameters();
    this.m_isDuringLoadStudy = true;
    CApAWCOpenStudyParameters openStudyParameters = new CApAWCOpenStudyParameters(input);
    if (this.CanLoadProcessToTune())
      this.m_openStudyParams = openStudyParameters;
    this.m_isDuringLoadStudy = false;
  }

  private void LoadStudyInPSViewer(TxHostLoadStudyMsg input)
  {
    this.ClearParameters();
    this.m_isDuringLoadStudy = true;
    this.m_openStudyParams = new CApAWCOpenStudyParameters(input);
    this.m_isDuringLoadStudy = false;
  }

  private void ClearParameters()
  {
    this.m_openStudyParams = (CApAWCOpenStudyParameters) null;
    this.m_loadStudyWasCanceled = false;
    this.m_isDuringLoadStudy = false;
  }

  private bool LoadProcessToTune()
  {
    bool tune = false;
    if (this.m_openStudyParams != null)
    {
      this.m_isDuringLoadStudy = true;
      if (!TxTcStudyReservation.Check(this.m_openStudyParams.StudyContextUid, this.m_openStudyParams.CollabContextUid, (TxTcAppRef) null))
      {
        this.m_loadStudyWasCanceled = true;
        return true;
      }
      Cursor overrideCursor = Mouse.OverrideCursor;
      Mouse.OverrideCursor = Cursors.Wait;
      try
      {
        if (TxApplication.ActiveDocument.PlatformGlobalServicesProvider is ITxTcGlobalServicesProvider servicesProvider)
          servicesProvider.LoadStudy(this.m_openStudyParams.StudyContextUid, this.m_openStudyParams.CollabContextUid, this.m_openStudyParams.Mode);
        tune = true;
      }
      catch (Exception ex)
      {
        TxApplication.LogWriter.WriteExceptionLine(ex);
      }
      finally
      {
        Mouse.OverrideCursor = overrideCursor;
        this.m_isDuringLoadStudy = false;
      }
    }
    return tune;
  }

  private bool CanLoadProcessToTune()
  {
    int num = 1;
    TxCommandManager txCommandManager = new TxCommandManager();
    CoExportToTcParams exportToTcParams = (CoExportToTcParams) new CoExportToTcParamsClass();
    ((ICoExportToTCParams) exportToTcParams).SetConfirmSave(1);
    if (txCommandManager.ExecuteCommandByCommandProgId("TCIntegCommands.CCoExportToTC", (object) exportToTcParams, false))
    {
      ((ICoExportToTCParams) exportToTcParams).GetCancel(ref num);
      this.m_loadStudyWasCanceled = num == 1;
    }
    return num == 0;
  }
}
